﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.Entities;
using OreSystem.BLL;
using OreSystem.Common;
namespace MainFrom {
    public partial class LoginForm : Form {
        private UserBLL userBLL = new UserBLL();
        public LoginForm() {
            InitializeComponent();
        }

        private void btn_login_Click(object sender, EventArgs e)
        {

            //MainFrom MForm = new MainFrom();
            //MForm.User = new User { UserName = "admin" };
            //MForm.Tag = this.FindForm();
            //MForm.Show();
            //this.Hide();
            //return;


            string userName = txt_username.Text;
            string passWord = txt_password.Text;
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("用户名不能空");
                return;
            }
            if (userBLL.IsLogin(userName, passWord))
            {
                MainFrom MForm = new MainFrom();
                MForm.User = userBLL.GetUserByUserName(userName);
                MForm.Tag = this.FindForm();
                MForm.Show();
                this.Hide();
                txt_password.Text = "";
                //this.Close();
            }
            else
            {
                MessageBox.Show("用户名或密码不对！");
                txt_username.Focus();
            }
        }       

        private void btn_reset_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("备份提示","是否要备份？",MessageBoxButtons.YesNo)== System.Windows.Forms.DialogResult.Yes)
            {
                new BackDataForm().ShowDialog();
            }
        }

        private void 配置设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new InitSystemForm().ShowDialog();
        }

        private void 附加数据库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new FormAttach_db().ShowDialog();
        }

        private void 重置密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string sql = @"use OreSystemData
if Exists( select * from T_User where UserName = 'admin')
begin
update T_User set PassWord='A47A40EFFCC8A631459D04E205888502' where UserName = 'admin'
end
else
begin
insert into T_User  values('admin','A47A40EFFCC8A631459D04E205888502',0,getDate());
end";
            try
            {
                MsSqlHelper.ExceuteNoQuery(sql);
                MessageBox.Show("重置密码成功密码为：admin");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void 还原数据库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new FormRestoreDataBase().ShowDialog();
        }
    }
}
